<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/3/14
 * Time: 13:08
 */

namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;


class BiHaiChaoSheng extends Common
{
     public static $Dr = [];
    //陈列
    public function AjaxCL(){
        $type = $this->request->param('type');
        $cl_type = [
                ['title'=>'货架陈列','id'=>'1'],
                ['title'=>'堆头陈列','id'=>'2'],
                ['title'=>'端架陈列','id'=>'3'],
                ['title'=>'包柱陈列','id'=>'4'],
                ['title'=>'挂网陈列','id'=>'5'],
                ['title'=>'其他陈列','id'=>'6'],
        ];
        $cl_place = [
            ['title'=>'主通道','id'=>'1'],
            ['title'=>'副通道','id'=>'2'],
            ['title'=>'收银台','id'=>'3'],
            ['title'=>'卖场门口','id'=>'4'],
            ['title'=>'端架两侧','id'=>'5'],
            ['title'=>'其他位置','id'=>'6'],
        ];
        $cl_size =[
            ['title'=>'1m x 1m','id'=>'1'],
            ['title'=>'2m x 1m','id'=>'2'],
            ['title'=>'2m x 2m','id'=>'3'],
            ['title'=>'4m x 1m','id'=>'4'],
            ['title'=>'整节端架','id'=>'5'],
            ['title'=>'半节端架','id'=>'6'],
            ['title'=>'其他尺寸','id'=>'7'],
        ];
        if($type==1){
            echo json_encode(['message'=>'陈列类型','value'=>$cl_type,'code'=>'200','redirect'=>'']);
            //echo json_encode(['message'=>'ok','value'=>$cl_type,'code'=>'200','redirect'=>'']);
        }elseif ($type==2){
            echo json_encode(['message'=>'陈列位置','value'=>$cl_place,'code'=>'200','redirect'=>'']);
            //echo json_encode(['message'=>'ok','value'=>$cl_place,'code'=>'200','redirect'=>'']);
        }else{
            echo json_encode(['message'=>'陈列尺寸','value'=>$cl_size,'code'=>'200','redirect'=>'']);
            //echo json_encode(['message'=>'ok','value'=>$cl_size,'code'=>'200','redirect'=>'']);
        }
    }
    //选择科目
    public function AjaxFYKU(){
        $getData = $this->request->param();
        //echo '222';
        //p($getData);
        $where = [];
        if(isset($getData['esuseway'])){
            $where['esuseway'] = ['like','%"'.$getData['esuseway'].'"%'];
        }
        if(isset($getData['channeltypes'])){
            $where['channeltypes'] = ['like','%"'.$getData['channeltypes'].'"%'];
        }

        $data = Db::table('cg_feiyongkemu3')->order("id desc")->where($where)->field('title,id,pid')->select();
        //p($data);
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //new tpm
    public function AjaxFYKU2(){
        $getData = $this->request->param();
        //echo '222';
        //p($getData);
        $where = [];
        if(isset($getData['esuseway'])){
            $where['esuseway'] = ['like','%"'.$getData['esuseway'].'"%'];
        }
        if(isset($getData['fylx'])){
            $where['freekmlx'] = ['in',$getData['fylx']];

        }
        if(isset($getData['channeltypes'])){
            $where['channeltypes'] = ['like','%"'.$getData['channeltypes'].'"%'];
        }
//p($where);
       // $data = Db::table('cg_feiyongkemu3')->order("id desc")->where($where)->field('title,id,pid')->select();
        $data = Db::table('cg_feiyongkemu3')->order("id desc")->where($where)->field('title,id,freekmlx as fylx')->select();
        //echo Db::table('cg_feiyongkemu3')->getLastSql();die;
        //p($data);
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //选择系统
    public function AjaxJXS(){
        $getData = $this->request->param();
        $where = [];
        if(isset($getData['jxs'])){
            $where['jxs'] = $getData['jxs'];
        }
        $data = Db::table('cg_system')->order("id desc")->where($where)->field('title,id')->select();
        foreach ($data as $k=>&$v){
            //$where['system_id'] = $v['id'];
            $v['num']=Db::table('cg_net')->order("id desc")->where(['system_id'=>$v['id']])->count();

        }
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //选择網點
    public function AjaxWD(){
        $getData = $this->request->param();
        $where = [];
        if(isset($getData['jxs'])){
            $where2['jxs'] = $getData['jxs'];
            $sysId = Db::table('cg_system')->order("id desc")->where($where2)->column('id');
            $where['system_id'] = ['in',$sysId];
        }
        $data = Db::table('cg_net')->order("id desc")->where($where)->field('title,id')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }
    //list 选择器组件
    public function selectList($str='',$url='',$where='',$type=''){

        $this->assign('str', $str);
        //p($this->setUrl($url,$where));   // /index/bi_hai_chao_sheng/getcp.html?type=1&style=5&jxs=1
        $this->assign('url', $this->setUrl($url,$where));
        if (isset($type)&&!empty($type)){
            $this->assign('type',$type);
            return $this->fetch('selectList2');
        }else{
            return $this->fetch();
        }

    }
    //设置Url
    public function setUrl($url,$where){
        //判断是否有条件
        $setGet = '';

        if(!empty($where)){
            $whereA = explode('__',$where);

            $setGet = '?';
            foreach ($whereA as $v){
                $whereB = explode('_',$v);

                $setGet.=$whereB[0].'='.$whereB[1].'&';
            }
            $setGet = substr($setGet,0,strlen($setGet)-1);
        }

        $retUrl = '';
        switch ($url){
            case 'Tifykm'://获取费用科目三 适用于Ti费用申请URL
                $retUrl = url('BiHaiChaoSheng/getTifylx');
                break;
            case 'myzonetreeajax':
                $retUrl = url("select/myzonetreeajax");
                break;
            case 'Tixt':
                $retUrl = url("BiHaiChaoSheng/getTixt");
                break;
            case 'Tiwd':
                $retUrl = url("BiHaiChaoSheng/getTiwd");
                break;
            case 'getCp':
                $retUrl = url("BiHaiChaoSheng/getCp");
                break;
        }
        return $retUrl.$setGet;

    }
    ////获取费用科目三 适用于Ti费用申请树结构数据
    public function getTifylx(){
        $getData = $this->request->param();
        $where = [];
        if(isset($getData['esuseway'])){
            $where['esuseway'] = ['like','%"'.$getData['esuseway'].'"%'];
        }
        if(isset($getData['fylx'])){
            $where['freekmlx'] = ['in',$getData['fylx']];

        }
        if(isset($getData['channeltypes'])){
            $where['channeltypes'] = ['like','%"'.$getData['channeltypes'].'"%'];
        }

        //获取三级分类树表
        $data = Db::table('cg_feiyongkemu3')->order("id desc")->where($where)->field('title as name,id,pid')->select();
        $fu = [];
        foreach ($data as $k=>$v){
            if(isset($fu[$v['pid']])){
                $a = $v;
                unset($a['pid']);

                $fu[$v['pid']]['children'][] = $a;
            }else{
                $cg_feiyongkemu2 = Db::name('cg_feiyongkemu2')->where('id',$v['pid'])->find();
                $fu[$v['pid']]['name'] = $cg_feiyongkemu2['title'];
                $a = $v;
                unset($a['pid']);
                $fu[$v['pid']]['children'][] = $a;
                $fu[$v['pid']]['chkDisabled'] = 1;
                $fu[$v['pid']]['nocheck'] = 1;

            }
        }
        $fu = array_merge($fu);
        echo json_encode($fu);
    }
    ////ti 系統
    public function getTixt(){
        $getData = $this->request->param();
        $where = [];
        if(isset($getData['jxs'])){
            $where['jxs'] = $getData['jxs'];
        }
       //获取三级分类树表
        $data = Db::table('cg_system')->order("id desc")->where($where)->field('title as name,id')->select();
        echo json_encode($data);
    }
    ////ti 網點
    public function getTiwd(){
        $getData = $this->request->param();
        $where = [];
        if(isset($getData['jxs'])){
            $where2['jxs'] = $getData['jxs'];
            $sysId = Db::table('cg_system')->order("id desc")->where($where2)->column('id');

            $where['system_id'] = ['in',$sysId];
           // p($where);
        }
       $data = Db::table('cg_net')->order("id desc")->where($where)->field('title as name,id,system_id as pid')->select();
        /*echo Db::table('cg_net')->getLastSql();die;
        p($data);*/

        $fu = [];
        foreach ($data as $k=>$v){
            if(isset($fu[$v['pid']])){
                $a = $v;
                unset($a['pid']);
                $fu[$v['pid']]['children'][] = $a;


            }else{
                $cg_feiyongkemu2 = Db::name('cg_system')->where('id',$v['pid'])->find();
                $fu[$v['pid']]['name'] = $cg_feiyongkemu2['title'];
                $a = $v;
                unset($a['pid']);
                $fu[$v['pid']]['children'][] = $a;
                $fu[$v['pid']]['chkDisabled'] = 1;
                $fu[$v['pid']]['nocheck'] = 1;

            }
        }

        $fu = array_merge($fu);

        echo json_encode($fu);
    }
    //获取所有经销商
    public function ajaxJXSALL(){
        $data = Db::table('cg_system')->field('title as text,id')->where("isdel=0")->select();
        echo json_encode($data);
    }
    //获取所有（经销商所在的）区域
    public function ajaxZoningALL(){
        $data = Db::table('cg_market_zoning')->field('title as text,id')->where("isdel=0")->select();
        echo json_encode($data);
    }
    //获取所有（经销商下的）网点
    public function ajaxNetALL(){
        $getData = $this->request->param();
        $where = [];
        if(isset($getData['jxs'])){
            $where2['jxs'] = $getData['jxs'];
            $sysId = Db::table('cg_system')->order("id desc")->where($where2)->column('id');
            $where['system_id'] = ['in',$sysId];
        }
        $data = Db::table('cg_net')->order("id desc")->where($where)->field('title as text,id')->select();

        /*$data = Db::table('cg_market_zoning')
            ->field('title as text,id')->where("isdel=0")->select();*/
        echo json_encode($data);
    }


    //获取三级科目下的对应支付方式 以及陈列方式
    public function get3KmDyfkfs(){
        $getData = $this->request->param();
        $payway = Db::name('cg_feiyongkemu3')->where('id',$getData['id'])->value('payway');
        $data = Db::name('cg_feiyongkemu3')->where('id',$getData['id'])->find();
        $redata = [];

        if(!empty($payway)){
            $payway = unserialize($payway);
            $where['id'] = ['in',$payway];
            $redata =  Db::name('cg_paymethod')->where($where)->field('method,id,title')->select();
        }
        if(empty($redata)){ // 如未设定则默认支持所有支付方式
            $redata = db('cg_paymethod')->field('method,id,title')->select();
        }
        echo json_encode(['message'=>'ok','value'=>$redata,'code'=>'200','redirect'=>'','chenlie'=>$data['chenlie'],'fylx'=>$data['freekmlx']]);//多陈列方式判断
    }
    ///////////////////////////////////////////////////
    //獲取產品
    //type : 1.樹形列表 2.下拉列表數據
    //sytle:1品牌 2.品類 3.系列 4.sku 5.單品
    //pid : 上级id
    //////////////////////////////////////////////////////////

    public function getCp(){
        $getData = $this->request->param(); //type  style
        //p($getData);
        $sytle = $getData['style'];
        $type = $getData['type'];

        $retData = [];
        $retData2 = [];
        $where1=["is_actived"=>1,"is_deleted"=>0];
        $brands = Db::name('pro_brands')->where($where1)->field('brand_name name,id')->select();


        $i = 0;
        $j = 0;


        if($brands){
            Db::startTrans();
            try {
            foreach ($brands as $v){
                if($sytle==1){
                    $retData2[$i]['title'] = $v['name'];
                    $retData2[$i]['id'] = $v['id'];
                    $retData[$v['id']]['name'] = $v['name'];
                    $retData[$v['id']]['id'] = $v['id'];
                    $i++;

                }elseif($sytle>1){
                    $retData[$v['id']]['name'] = $v['name'];
                    $retData[$v['id']]['chkDisabled'] = 1;
                    $retData[$v['id']]['nocheck'] = 1;
                    $retData[$v['id']]['children'] = [];
                }
                $where2['brand_id'] = $v['id'];
                $category = Db::name("pro_category")
                    ->where($where1)->where($where2)->field('category_name as name,id')
                    ->select();

                if($category){
                    foreach ($category as $v1){
                        if($sytle==2){
                            $retData2[$i]['title'] = $v1['name'];
                            $retData2[$i]['id'] = $v1['id'];
                            $retData[$v['id']]['children'][$v1['id']]['name'] = $v1['name'];
                            $retData[$v['id']]['children'][$v1['id']]['id'] = $v1['id'];
                            $i++;
                        }elseif($sytle>2){
                            $retData[$v['id']]['children'][$v1['id']]['name'] = $v1['name'];
                            $retData[$v['id']]['children'][$v1['id']]['chkDisabled'] = 1;
                            $retData[$v['id']]['children'][$v1['id']]['nocheck'] = 1;
                            $retData[$v['id']]['children'][$v1['id']]['children'] = [];
                        }
                        $where3['category_id'] = $v1['id'];
                        $pro_series = Db::name("pro_series")->where($where1)->where($where3)
                            ->field('series_name as name ,id')
                            ->select();
                        if($pro_series){
                            foreach ($pro_series as $v2){
                                if($sytle==3){
                                    $retData2[$i]['title'] = $v2['name'];
                                    $retData2[$i]['id'] = $v2['id'];
                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['name'] = $v2['name'];
                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['id'] = $v2['id'];
                                    $i++;

                                }elseif($sytle>3){
                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['name'] = $v2['name'];
                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['chkDisabled'] = 1;
                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['nocheck'] = 1;
                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'] = [];
                                }
                                $where4['series_id'] = $v2['id'];
                                $pro_sku = Db::name("pro_sku")->where($where1)->where($where4)
                                    ->field('sku_name as name,id')
                                    ->select();
                                if($pro_sku){
                                    foreach ($pro_sku as $v3){
                                        if($sytle==4){
                                            $retData2[$i]['title'] = $v3['name'];
                                            $retData2[$i]['id'] = $v3['id'];
                                            $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['name'] = $v3['name'];
                                            $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['id'] = $v3['id'];
                                            $i++;

                                        }elseif($sytle>4){
                                            $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['name'] = $v3['name'];
                                            $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['chkDisabled'] = 1;
                                            $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['nocheck'] = 1;
                                            $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'] = [];
                                        }
                                        $where5['sku_id'] = $v3['id'];
                                        if(isset($getData['jxs'])){
                                            $where12=["p.is_actived"=>1,"p.is_deleted"=>0];
                                            $where52['p.sku_id'] = $v3['id'];
                                            $pro_products = Db::name("pro_products")->alias("p")
                                                ->join("pro_price pp", "pp.pid=p.id") //pro_price
                                                ->field('p.id,p.pro_name as name,p.box_num,p.unit,pp.factory,pp.box_factory,pp.box_retail_standard as xtxgj,pp.box_sell_standard as yls')//这个加factory 有问题的
                                                ->where($where52)                    // box_retail_standard  供货价
                                                ->where($where12)
                                                ->select();
                                            //box_retail_standard 系统/网点原供价   box_sell_standard 零售价
                                        }else{
                                            $pro_products = Db::name("pro_products")
                                                ->field('pro_name as name,id')
                                                ->where($where5)
                                                ->where($where1)
                                                ->select();
                                        }
                                        if($sytle==5){
                                            foreach ($pro_products as $v4){
                                               // p($getData);
                                                if(isset($getData['jxs'])){
                                                    $jxs=$getData['jxs'];
                                                   // p($jxs);
                                                    $id=$v4['id'];
                                                    $gp = Db::table("price_group_product")->alias("pgp")
                                                        ->join("price_group_jxs pgj", "pgp.gid=pgj.gid", "LEFT")
                                                        ->where("pgj.jid=$jxs and pgp.pid=$id")
                                                        ->field("pgp.lingshou_price,pgp.fayun_price")->find();
                                                    $jc =Db::table("pro_price")->alias("pp")
                                                        ->where("pp.pid=$id")
                                                        ->field("pp.retail_standard as lssjg,pp.sell_standard as mcjg")->find();
                                                    if ($gp){
                                                        $price = $gp["fayun_price"];
                                                    }else{
                                                        $price = $v4["box_factory"]; //box_factory
                                                    }

                                                }else{

                                                }

                                                $retData2[$i]['title'] = $v4['name'];
                                                $retData2[$i]['id'] = $v4['id'];
                                                ////原供价

                                                $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['name'] = $v4['name'];
                                                $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['id'] = $v4['id'];
                                                if(isset($getData['jxs'])){
                                                    $retData2[$i]['xtxgj'] = $v4['xtxgj'];
                                                    $retData2[$i]['yls'] = $v4['yls'];
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['xtxgj'] = $v4['xtxgj'];
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['yls'] = $v4['yls'];

                                                    $retData2[$i]['box_num'] = $v4['box_num'];//box_num,unit
                                                    $retData2[$i]['unit'] = $v4['unit'];
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['box_num'] = $v4['box_num'];
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['unit'] = $v4['unit'];

                                                    $retData2[$i]['price'] = $price;
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['price'] = $price;

                                                    //进场类
                                                    $retData2[$i]['lssjg'] = $jc['lssjg'];
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['lssjg'] = $jc['lssjg'];
                                                    $retData2[$i]['mcjg'] = $jc['mcjg'];
                                                    $retData[$v['id']]['children'][$v1['id']]['children'][$v2['id']]['children'][$v3['id']]['children'][$v4['id']]['mcjg'] = $jc['mcjg'];
                                                }
                                                $i++;
                                            }
                                            //p($retData2);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }

            }   //factory
        } catch (\Exception $e) {
            Db::rollBack();
                return $e->getMessage();
            return json_encode(["wrong" => "系统繁忙请稍后再试", "aid" => ""]);
        }

        }
//echo 'uuu';
       // p($retData);
        if($type==1){
         for ($p = 0;$p<$sytle;$p++){
             $retData = $this->foo($retData);
         }

        echo json_encode($retData);
        }elseif ($type==2){
            echo json_encode(['message'=>'ok','value'=>$retData2,'code'=>'200','redirect'=>'']);
        }
    }
    //修改list 返回格式 arr 数组 ，style
    function foo(&$ar) {
        foreach ($ar as $k=>&$v){
            if(isset($v['children'])&&is_array($v['children'])){
                if(empty($v['children'])){
                    unset($ar[$k]);
                }else{
                    $v['children'] = $this->foo($v['children']);
                }
            }
        }
        $ar = array_values($ar);
        return $ar;
    }
    //判断指定数组是否为空 为空删除 主要设置时间、金额等字段的问题

    /**
     * @setKey  查询的值
     * @data 原始数据
     * @is 是否启用
     */
    static public function setData($data,$setKey,$is=1){
        if(!$is){
            foreach ($setKey as $v){
                if(isset($data[$v])&&empty($data[$v])){
                    unset($data[$v]);
                }
            }
            return $data;
        }else{
            return $data;
        }

    }
    /**
    *获取供应商
    */
    function getGYS(){
        $data = Db::name('gys')->where('is_actived',1)->where('is_deleted',0)->field('gys_name as title,id')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

}
